package com.zhixi.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Author zhangzhixi
 * @Description
 * @Date 2022-12-19 17:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class LoginController {


    @PostMapping("login")
    public String login(@RequestParam("user_name") String userName, @RequestParam("user_pwd") String userPasswd) {
        if ("zhangsan".equals(userName) && "123".equals(userPasswd)) {
            return "登录成功";
        } else {
            return "登录失败";
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 严格限制日期转换
        sdf.setLenient(false);
        // true:允许输入空值，false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    @GetMapping("bind/{userName}/{birthday}")
    public Map<String, Object> enroll(@PathVariable("userName") String userName,
                                      @PathVariable("birthday") Date birthday) {
        Map<String, Object> map = new HashMap<>();
        map.put("userName", userName);
        map.put("birthday", birthday);
        return map;
    }
}
